using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LogFileViewer.Properties;

namespace LogFileViewer
{
    public partial class LogEntryView : Form
    {
        public LogEntryView(LogEntry entry, int columnIndex, int arrayLength)
        {
            InitializeComponent();
            this.entry = entry;
            this.columnIndex = columnIndex;
            this.arrayLength = arrayLength;
        }

        private static Settings sets = Settings.Default;

        private LogEntry entry;
        private int columnIndex;
        private int arrayLength;

        private void LogEntryView_Load(object sender, EventArgs e)
        {
            textDate.Text = this.entry.Datetime.ToLongDateString() + "  (" + this.entry.Datetime.ToLongTimeString() + ")";

            richTextBox.AppendText(this.entry.Package);
            richTextBox.AppendText("\n" + this.entry.Message);

            if (this.entry.Type == MessageTypes.INFO)
            {
                pictureType.Image = Resources._pic_info;
            }
            else if (this.entry.Type == MessageTypes.DEBUG)
            {
                pictureType.Image = Resources._pic_debug;
            }
            else if (this.entry.Type == MessageTypes.WARN)
            {
                pictureType.Image = Resources._pic_warn;
            }
            else if (this.entry.Type == MessageTypes.ERROR)
            {
                pictureType.Image = Resources._pic_error;
            }
            else if (this.entry.Type == MessageTypes.FATAL)
            {
                pictureType.Image = Resources._pic_fatal;
            }

            toolStripStatusLblNumber.Text = (columnIndex + 1).ToString();
            toolStripStatusLblMax.Text = arrayLength.ToString();

            Font fnt = new Font(richTextBox.Font.FontFamily, richTextBox.Font.Size, FontStyle.Bold, GraphicsUnit.Point);

            // Format package declaration (blue)
            int pos = richTextBox.Find(this.entry.Package);
            if (pos > -1)
            {
                richTextBox.SelectionStart = pos;
                richTextBox.SelectionLength = this.entry.Package.Length;
                richTextBox.SelectionColor = Color.Blue;
                richTextBox.SelectionFont = fnt;
            }
            richTextBox.SelectionLength = 0;

            // SETTING WINDOW POSITION & SIZE
            if (sets.LogEntryViewPosX == 0 || sets.LogEntryViewPosY == 0)
            {
                sets.LogEntryViewPosX = this.Bounds.X;
                sets.LogEntryViewPosY = this.Bounds.Y;
            }
            this.Bounds = new Rectangle(sets.LogEntryViewPosX, sets.LogEntryViewPosY, sets.LogEntryViewWidth, sets.LogEntryViewHeight);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LogEntryView_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Setting window size & position
            sets.LogEntryViewWidth = this.Width;
            sets.LogEntryViewHeight = this.Height;
            sets.LogEntryViewPosX = this.Bounds.X;
            sets.LogEntryViewPosY = this.Bounds.Y;
            sets.Save();
        }
    }
}